package com.toune.movie.mvp.model;

import android.util.Log;

import com.toune.movie.base.Api;
import com.toune.movie.mvp.contract.MovieMainContract;
import com.toune.movie.mvp.presenter.MovieMainPresenter;
import com.toune.movie.vo.VarietyVo;
import com.vondear.rxtool.RxThreadPoolTool;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/9.
 */

public class MovieMainModel implements MovieMainContract.Model {
    MovieMainPresenter movieMainPresenter;

    public MovieMainModel(MovieMainPresenter movieMainPresenter) {
        this.movieMainPresenter = movieMainPresenter;
    }

    @Override
    public void getVo(final int page) {
        new RxThreadPoolTool(RxThreadPoolTool.Type.CachedThread, 1).execute(new Runnable() {
            @Override
            public void run() {
                paseHtml("http://www.a2yy.com/");
            }
        });
    }

    private void paseHtml(String url) {
        try {
            //还是一样先从一个URL加载一个Document对象。
            Document doc = Jsoup.connect(url).get();
            Elements contents = doc.getElementsByClass("v_list_ul");
            List<VarietyVo> varietyVoList = new ArrayList<>();
            for (int i = 0; i < contents.get(0).childNodes().size(); i++) {
                if (i > 11) {
                    break;
                }
                //一个li
                Element element = contents.get(0).child(i);
                //li下面的pic模块
                Element pic = doc.select("div.pic").get(i);
                Element item = pic.child(0);
                String imagSrc = item.attr("src");//图片链接
                String title = item.attr("alt");//标题
                Elements a = element.getElementsByTag("a");
                String detailUrl = a.get(0).attr("href");
                detailUrl = Api.MAIN_NAME + detailUrl;
                String pingfen = element.getElementsByClass("duration-nobg").get(0).text();
                VarietyVo varietyVo = new VarietyVo();
                varietyVo.setImagSrc(imagSrc);
                varietyVo.setDetailUrl(detailUrl);
                varietyVo.setTitle(title);
                varietyVo.setPingfen(pingfen);
                varietyVoList.add(varietyVo);
            }
            movieMainPresenter.setVo(varietyVoList);


            //获取头部宣传
            List<VarietyVo> heandersVo = new ArrayList<>();
            Elements headers = doc.select("div.h_slide_container");
            Elements aHeaders = headers.get(0).getElementsByTag("a");
            for (int i = 0; i < aHeaders.size(); i++) {
                VarietyVo varietyVo = new VarietyVo();
                String detailUrl = aHeaders.get(i).attr("href");
                detailUrl = Api.MAIN_NAME + detailUrl;
                varietyVo.setDetailUrl(detailUrl);
                String title = aHeaders.get(i).attr("title");
                varietyVo.setTitle(title);
                Elements imageClass = aHeaders.get(i).getElementsByTag("img");
                String imageUrl = imageClass.get(0).attr("src");
                varietyVo.setImagSrc(imageUrl);
                heandersVo.add(varietyVo);
            }
            movieMainPresenter.setHeaders(heandersVo);
        } catch (Exception e) {
            Log.i("mytag", e.toString());
        }
    }
}
